package com.happystreet.manager
{
	import flash.events.NetStatusEvent;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;

	/**
	 * 本地缓存管理
	 * @author Liseen
	 * 
	 */	
	public class SettingsManager
	{
		public static const QUALITY_SETTINGS:String = "qualitySettings";
		public static const SOUND_SETTINGS:String = "soundSettings";
		public static const BGM_SETTINGS:String = "bgmSettings";
		public static const MULTILOVE_LEVEL:String = "multiLoveLevel";
		public static const JOB_LIST_STATE:String = "jobListState";
		
		private static var userSharedObject:SharedObject;
		private static var sharedObjectPath:String;
		
		public function SettingsManager()
		{
		}
		
		public static function loadParam(_arg1:String):Object
		{
			userSharedObject = SharedObject.getLocal(_arg1, sharedObjectPath);
			trace("get param " + _arg1 + " > value = " + userSharedObject.data.param);
			return (userSharedObject.data.param);
		}
		
		public static function saveParam(param_name:String, setting_value:Object):void
		{
			userSharedObject = SharedObject.getLocal(param_name, sharedObjectPath);
			userSharedObject.data.param = setting_value;
			var flushStatus:String;
			try
			{
				flushStatus = userSharedObject.flush();
			}
			catch(error:Error)
			{
				trace("Error...Could not write SharedObject to disk\n");
			}
			if (flushStatus != null)
			{
				switch (flushStatus)
				{
					case SharedObjectFlushStatus.PENDING:
						userSharedObject.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus, false, 0, true);
						return;
					case SharedObjectFlushStatus.FLUSHED:
						return;
				}
			}
		}
		
		private static function onFlushStatus(_arg1:NetStatusEvent):void
		{
			trace("User closed permission dialog...\n");
			//Kraken.ktrace("User closed permission dialog...\n");
			switch (_arg1.info.code)
			{
				case "SharedObject.Flush.Success":
					trace("User granted permission -- value saved.\n");
					break;
				case "SharedObject.Flush.Failed":
					trace("User denied permission -- value not saved.\n");
					break;
			}
			userSharedObject.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
		}
		
		public static function setLocalSharedObjectPath(path:String):void
		{
			if (path.indexOf("http://") < 0)
			{
				return;
			}
			sharedObjectPath = "/";
		}
	}
}